package com.cn.chaomeng.controller;

import com.cn.chaomeng.constant.SysParam;
import com.cn.chaomeng.dto.FoodRetDTO;
import com.cn.chaomeng.dto.ResultDTO;
import com.cn.chaomeng.model.Food;
import com.cn.chaomeng.model.Foodtype;
import com.cn.chaomeng.model.Orders;
import com.cn.chaomeng.service.ExChangeService;
import com.cn.chaomeng.vo.app.OrderInfosVO;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 范中燊
 * @version V1.0
 * @Title: ${file_name}
 * @Package ${package_name}
 * @Description: TODO
 * @date ${date} ${time}
 */
@Controller
@RequestMapping("index")
public class IndexController
{

    private final Logger logger = Logger.getLogger(IndexController.class);

    @Autowired
    private ExChangeService exChangeService;

    @RequestMapping("/test")
    public ModelAndView doTest(HttpServletRequest request)
        throws IOException
    {
        Map<String, Object> resMap = new HashMap<String, Object>();
        return new ModelAndView("index", resMap);
    }

    /**
     * 点餐页面
     *
     * @param request
     * @return
     */
    @RequestMapping("/show")
    public ModelAndView doShow(HttpServletRequest request)
    {
        String openId = request.getParameter("openId");

        Map<String, Object> resMap = new HashMap<String, Object>();

        Map<String, Object> selectMap = new HashMap<String, Object>();

        List<Foodtype> foodTypeList = exChangeService.getFoodTypeList(selectMap);

//        List<Food> foodList = exChangeService.getFoodList(selectMap);
        List<FoodRetDTO> foodList = exChangeService.getFoodSales(selectMap);

        resMap.put("foodTypeList", foodTypeList);
        resMap.put("foodList", foodList);
        resMap.put("openId", openId);

        return new ModelAndView("app/index", resMap);
    }

    /**
     * 结算页面
     *
     * @param openId
     * @param orderid
     * @return
     * @throws IOException
     */
    @RequestMapping("/pay")
    public ModelAndView payer(@RequestParam String openId, @RequestParam int orderid)
        throws IOException
    {
        Map<String, Object> resMap = new HashMap<String, Object>();

        Orders orders = exChangeService.getOrdersByKey(orderid);

        List<OrderInfosVO> orderInfosVOs = exChangeService.getOrderInfos(orderid);

        resMap.put("orders", orders);
        resMap.put("openId", openId);
        resMap.put("orderInfos", orderInfosVOs);

        return new ModelAndView("app/payer", resMap);
    }

    /**
     * 历史订单页面
     *
     * @param openId
     * @return
     */
    @RequestMapping("/history")
    public ModelAndView OrderHistory(@RequestParam String openId)
    {
        Map<String, Object> resMap = new HashMap<String, Object>();

        resMap.put("openId", openId);

        return new ModelAndView("app/index_ed", resMap);
    }

    @RequestMapping("/delOrder")
    @ResponseBody
    public ResultDTO delOrder(@RequestParam String orderid)
    {

        ResultDTO resultDTO = new ResultDTO();

        Map<String, Object> selectMap = new HashMap<String, Object>();

        selectMap.put("orderid", orderid);
        selectMap.put("state", SysParam.FAIL_PAY_STATE);

        exChangeService.setOrderState(selectMap);

        resultDTO.setResult(true);
        resultDTO.setCode(200);
//        resultDTO.setData(foodList);
        return resultDTO;
    }

    /**
     * 获取订单历史
     *
     * @param openId
     * @return
     */
    @RequestMapping("/getOrders")
    @ResponseBody
    public ResultDTO getOrders(@RequestParam String openId)
    {

        ResultDTO resultDTO = new ResultDTO();

        Map<String, Object> selectMap = new HashMap<String, Object>();

        selectMap.put("openId", openId);

        Map<String, Object> obj = exChangeService.getOrderInfoByMap(selectMap);

        resultDTO.setResult(true);
        resultDTO.setCode(200);
        resultDTO.setData(obj);
        return resultDTO;
    }

    /**
     * 选择商品类别
     *
     * @param typeid
     * @return
     */
    @RequestMapping("/getList")
    @ResponseBody
    public ResultDTO getList(@RequestParam String typeid)
    {

        ResultDTO resultDTO = new ResultDTO();

        Map<String, Object> selectMap = new HashMap<String, Object>();

        selectMap.put("typeid", typeid);

        List<Food> foodList = exChangeService.getFoodList(selectMap);

        resultDTO.setResult(true);
        resultDTO.setCode(200);
        resultDTO.setData(foodList);
        return resultDTO;
    }

}
